﻿GroupAdd,special,ahk_class TTOTAL_CMD
GroupAdd,special,ahk_exe TickTick.exe
global OCR_IBEAM := 32513 , OCR_NORMAL := 32512
global Hznconfig:="Hznconfig.ini" 
;*************************************   功能开关 改为0就可以关掉   ***********************
CurTip:=1               ;想禁用指针切换改为0
ImTip:=1				;相禁用指示器改为0

;************************************************************


;*************************************   快捷键,前面加上 ;可以禁用   ***********************

Hotkey,!s,万能键   ; Alt+s万能键可以自己改按键
Hotkey, #F1, toggle_Pin ; win+f1快速切换双拼和全拼
Hotkey, #F12,srfreload ; win+F12重启影子
Hotkey, #F2, option		;win+F2打开设置
Hotkey, #F3, cikuManager ;win+F3打开词库管理
Hotkey, !z, CreateWords ;Alt+z造词
Hotkey, #F4, Plugins_Gui	;win+F4打开插件管理器,插件直接放根目录即可
; Hotkey, #F6, Hzn_SettingsGui

Hotkey If, srf_inputing
	Hotkey, ^space, 上屏搜索 ;Ctrl+space上屏的同时进行回车搜索
	Hotkey, ^+f, Changefj 	;Ctrl+Shift+f切换繁体
	Hotkey, F1, Changefzm	;F1在有搜索框的情况下只显示辅助码的候选
	Hotkey,^BackSpace,Clearbm	;Ctrl+backspace清空输入框

Hotkey If

;************************************* ***********************

if (ImTip=1)
	SetTimer, ImTip, -1000
if (CurTip=1)
	SetTimer, CurToggle, -100

Goto, _init_
;************************************* 下面不懂尽量别动***********************
Hzn_SettingsGui: ;备用设置gui
Gui hs:Add, CheckBox,Checked vCurTip gCurToggle, whether open the CurTip method?
gui hs:show,,Hzn_SettingsGui
gui hs:Submit,noHide
Return
reCur:
	SPI_SETCURSORS := 0x57
	DllCall( "SystemParametersInfo", UInt,SPI_SETCURSORS, UInt,0, UInt,0, UInt,0 )
Return

CurToggle:
gui hs:Submit,noHide
if (CurTip=0){
	Gosub, reCur
}
Else
	gosub ChangeCur
Return

ChangeCurCn:
Cur_IBEAM := DllCall("LoadCursorFromFile", "Str",".\Cur\IBEAM_Cn.cur", "Ptr")
Cur_NORMAL := DllCall("LoadCursorFromFile", "Str",".\Cur\NORMAL_Cn.cur", "Ptr")
DllCall("SetSystemCursor", "Ptr", Cur_IBEAM, "Int", OCR_IBEAM)
DllCall("SetSystemCursor", "Ptr", Cur_NORMAL, "Int", OCR_NORMAL)
Return
ChangeCurEn:
Cur_IBEAM := DllCall("LoadCursorFromFile", "Str",".\Cur\IBEAM_En.cur", "Ptr")
Cur_NORMAL := DllCall("LoadCursorFromFile", "Str",".\Cur\NORMAL_En.cur", "Ptr")
DllCall("SetSystemCursor", "Ptr", Cur_IBEAM, "Int", OCR_IBEAM)
DllCall("SetSystemCursor", "Ptr", Cur_NORMAL, "Int", OCR_NORMAL)
Return
ChangeCur:
Cur_IBEAM_Cn := DllCall("LoadCursorFromFile", "Str",".\Cur\IBEAM_Cn.cur", "Ptr")
Cur_IBEAM_En := DllCall("LoadCursorFromFile", "Str",".\Cur\IBEAM_En.cur", "Ptr")
Cur_IBEAM_Ca := DllCall("LoadCursorFromFile", "Str",".\Cur\IBEAM_Ca.cur", "Ptr")
Cur_NORMAL_Cn := DllCall("LoadCursorFromFile", "Str",".\Cur\NORMAL_Cn.cur", "Ptr")
Cur_NORMAL_En := DllCall("LoadCursorFromFile", "Str",".\Cur\NORMAL_En.cur", "Ptr")
Cur_NORMAL_Ca := DllCall("LoadCursorFromFile", "Str",".\Cur\NORMAL_Ca.cur", "Ptr")
if (CurTip=1){
	vltd := GetKeyState("CapsLock", "T") ? "Ca" : srf_mode? "Cn" : "En"
	Cur_IBEAM := "Cur_IBEAM_" vltd
	Cur_NORMAL := "Cur_NORMAL_" vltd
	DllCall("SetSystemCursor", "Ptr", %Cur_IBEAM%, "Int", OCR_IBEAM)
	DllCall("SetSystemCursor", "Ptr", %Cur_NORMAL%, "Int", OCR_NORMAL)
}
Return

toggle_Pin:
	(Inputscheme = "pinyin") ? Inputscheme:= "xhsp" : Inputscheme:= "pinyin"
	if (Inputscheme = "pinyin")
		fuzhuma:=0 , 显示方案 :="全"	
	Else
		fuzhuma:=1 , 显示方案 :="鹤"	
gosub pinyinlei
GuiControl, Tip:Text, 输入方案, %显示方案%
Return


pinyinlei:
	shurulei:="pinyin"
	GuiControl, 3:, _Inputscheme, %pinyinlist%
	If !(Inputscheme~="pinyin|sp$"){
		tvar:=""
		For Value,Key In ["decfre","fixedword","Imagine","Learning","Tofirst","Wordfrequency"] {
			tvar .= Yzimeini["Settings",key]
			GuiControl, 3:, %key%, % (Yzimeini["Settings",key]:=%key%:=(SubStr(Yzimeini["Settings","Settingsbak"], A_Index, 1)?1:0))
		}
		tvar .= Inputscheme, Inputscheme:=SubStr(Yzimeini["Settings","Settingsbak"], 7)?SubStr(Yzimeini["Settings","Settingsbak"], 7):"pinyin"
		Yzimeini["Settings","Settingsbak"]:=tvar
	}
	_Inputscheme:=pinyinec[Inputscheme]
	GuiControl, 3:ChooseString, _Inputscheme, %_Inputscheme%
	GuiControl, % "3:Disable" (Inputscheme="pinyin"), Showquanpin
	Loop 15
		GuiControl, % "3:Show" (A_Index<9), % "Button" (A_Index+15)
	GuiControl, 3:Text, Imagine, 联想
	GuiControl, 3:Text, Learning, 自学习
	Yzimeini.Settings["Inputscheme"]:=Inputscheme
return
RemoveToolTip:
	ToolTip
Return

ImTip:
IniRead, Gui_x, % A_WorkingDir "\Set.ini", GuiHotkey, Gui_x
IniRead, Gui_y, % A_WorkingDir "\Set.ini", GuiHotkey, Gui_y
Gui_x := ((Gui_x = "ERROR") || !(Gui_x)) ? "x" RegExReplace(A_ScreenWidth/2,"\..+") : Gui_x
Gui_y := ((Gui_y = "ERROR") || !(Gui_y)) ? "y" RegExReplace(A_ScreenHeight/2,"\..+") : Gui_y

显示方案:=(Inputscheme ~= "xhsp") ? "鹤" : "全"
LangStatus := (srf_mode = 0) ? "英" : "中"
辅助码:= (fuzhuma = 0) ? "关" : "开"
Gui, Tip:+Owner -Caption +AlwaysOnTop +HwndZYGui border -DPIScale
Gui, Tip:font, s10, 微软雅黑
Gui, Tip:Margin,6,4
Gui,Tip:Add, Picture,gHideImTip , .\tb.png
Gui, Tip:Add, Text, y0 vLangStatus gLangStatus, %LangStatus%
Gui, Tip:Add, Text, x+5, |
Gui, Tip:Add, Text, x+5 v输入方案 g输入方案, %显示方案%
Gui, Tip:Add, Text, x+5, |
Gui, Tip:Add, Text, x+5 v辅助码 g辅助码, %辅助码%
Gui, Tip:Add, Text, x+5, |
Gui, Tip:Add, Text, x+5 v卡 gSolveKeyProblem,卡
Gui, Tip:show,%Gui_x% %Gui_y%
; OnMessage(0x201,"ImTip位置")
return
ImTip位置(){
	Global ZYGui
	; 用于移动Gui
    PostMessage, 0xA1, 2
	; 等待左键释放
	KeyWait, LButton
	; 获取ImTipGui信息
	WinGetpos, Gui_x, Gui_y, Gui_w, Gui_h, ahk_id %ZYGui%
	; 记录相应信息到ini
	IniWrite, % "x" Gui_x, % A_WorkingDir "\Set.ini", GuiHotkey, Gui_x
	IniWrite, % "y" Gui_y, % A_WorkingDir "\Set.ini", GuiHotkey, Gui_y
}
WriteLocation:
; remember the location
KeyWait, LButton
WinGetpos, Gui_x, Gui_y, Gui_w, Gui_h, ahk_id %ZYGui%
IniWrite, % "x" Gui_x, % A_WorkingDir "\Set.ini", GuiHotkey, Gui_x
IniWrite, % "y" Gui_y, % A_WorkingDir "\Set.ini", GuiHotkey, Gui_y
Return

HideImTip:
Gui, Tip:Hide
Return


LangStatus:
srf_mode := (srf_mode = 0) ? 1 : 0
LangStatus := (srf_mode = 0) ? "英" : "中"
GuiControl, Tip:Text, LangStatus, %LangStatus%
SetYzLogo(srf_mode, 1)
Gosub, ChangeCur
return

输入方案:
PostMessage, 0xA1, 2, Ahk_id %ZYGui%
if (A_GuiEvent = "DoubleClick"){
	(Inputscheme = "pinyin") ? Inputscheme:= "xhsp" : Inputscheme:= "pinyin"
	if (Inputscheme = "pinyin"){
		显示方案 :="全"
		fuzhuma:=0
		辅助码:="关"
	}
	Else{
		显示方案 :="鹤"
		fuzhuma:=1
		辅助码:="开"
		gosub pinyinlei
	}
GuiControl, Tip:Text, 输入方案, %显示方案%
GuiControl, Tip:Text, 辅助码, %辅助码%
}
Gosub, WriteLocation
Return

辅助码:
PostMessage, 0xA1, 2, Ahk_id %ZYGui%
	if (A_GuiEvent = "DoubleClick"){
	(fuzhuma=0) ? fuzhuma:=1 : fuzhuma:=0
	if (fuzhuma=0){
		辅助码:="关"
		gosub pinyinlei
	}
	else{
		辅助码:="开"
	GuiControl, Tip:Text, 辅助码, %辅助码%
	}
Gosub, WriteLocation
}
Return

万能键:
Gosub, up3
if  WinActive("ahk_group special")
	Gosub, SpecialFlow
Else
	Gosub,NormalFlow
Gosub, up3
Return

NormalFlow:
if (srf_inputing!=0)
	gosub Clearbm
Else{
Clipboard := ""
SendInput, +{Home}^c
ClipWait,1,1
SendInput,{Right}
if (RegExMatch(Clipboard, "(?<!\w)(\w+$)", 编码)) {
	Gosub, ChangeImeCn
	Gosub, ChangeCur
	编码长 := StrLen(编码)
	SendInput, % "{bs " 编码长 "}"
	srf_all_Input := 编码
} Else {
	Gosub, ChangeImeCn
	Gosub, ChangeCur
	编码长 := StrLen(srf_last_input[2,2])
	SendInput, % "{bs " 编码长 "}"
	srf_all_Input := srf_last_input[2,0]
}
	Begininput()
	Gosub srf_tooltip
}
return



SpecialFlow:
if (srf_inputing!=0){
gosub Clearbm
}Else{
;向后复制测试是否有东西
Clipboard :=firststr:=str1:=str2:=str3:=编码长:=编码:=""
SendInput, +{end}^c
sleep 100
laststr:=Clipboard , Clipboard :=""
;第一次向前复制
SendInput, +{Home}^c
ClipWait,1,1
; sleep 100
firststr:=Clipboard , str1:=StrLen(Clipboard) , Clipboard :=""
; ToolTip,%firststr%`,%laststr%	
;检测后面是否有内容
if (laststr="" or laststr=firststr)
	SendInput,{End}
Else
	SendInput, {right}
;第二次复制对比
; Clipboard := ""
; SendInput, +{Home}
; SendInput, ^c
; ClipWait, 1
; str2:=StrLen(Clipboard) ,str3:=Abs(str2-str1)-1
; SendInput,{Right 2}
; ToolTip,str1为%str1%`,str2为%str2%`,str3为%str3%
; if (str1!=str2){
; 	if (str3>0)
; 		SendInput,{Right %str3%}
; }
; if (str3<0)
; 		SendInput,{left}
if (RegExMatch(firststr, "(?<!\w)(\w+$)", 编码)) {
	Gosub, ChangeImeCn
	Gosub, ChangeCur
	编码长 := StrLen(编码)
	; MsgBox,% 编码长
	SendInput, % "{bs " 编码长 "}"
	srf_all_Input := 编码
} Else {
	Gosub, ChangeImeCn
	编码长 := StrLen(srf_last_input[2,2])
	Send, % "{bs " 编码长 "}"
	srf_all_Input := srf_last_input[2,0]
}
	Begininput()
	Gosub srf_tooltip
}
Return




ChangeImeCn:
	;确认输入法为中文
	SetYzLogo(srf_mode:=1, A_ThisLabel="srfSwitch")
	LangStatus:="中"
	GuiControl, Tip:Text, LangStatus, %LangStatus%
Return
上屏搜索:
	If srf_symbol[srf_all_Input] {
		RegExMatch(jichu_for_select_Array[localpos,2],"i)\{[a-z]+\}",Match)
		SendInput(StrReplace(jichu_for_select_Array[localpos,2],Match), SendDelaymode)
		If (Match)
			SendInput, %Match%
		Gosub srf_value_off
	} Else {
		If (dwxg&&ToolTipStyle=2&&localpos=1&&(jichu_for_select_Array[1,-2]=""&&jichu_for_select_Array[1,0]~="pinyin"&&StrLen(jichu_for_select_Array[1,2])>1)){
			If Showdwxgtip()
				Return
		}
		select_for_num(localpos)
	}
	SendInput,{enter}
Return
Changefj:
Changefj()
Return
Changefj(){  ;Traditional chinese and sample 
	Global
	Traditional := (Traditional = 0) ? 1 : 0
	valueindex:=Traditional?5:2
	Gosub houchuli
	Gosub srf_tooltip_fanye
	if (Traditional = 0)
	{
		ToolTip, simplified Chinese
		SetTimer, removeToolTip, -2000
	}
	Else
	{
		ToolTip, Traditional Chinese
		SetTimer, removeToolTip, -2000
	}
	Return
	}



Changefzm:
	If RegExMatch(srf_all_Input_["tip"],"O).*[a-z][a-z]'([a-z]('?[a-z])?)$",fzm){
	tfzm:=StrReplace(fzm.Value[1],"'"), srf_all_Input:=RegExReplace(srf_all_Input, tfzm "$")
	jichu_for_select_Array:=pinyinmethod(srf_all_Input, Inputscheme), waitnum:=0
	Gosub houchuli
	Gosub srf_tooltip_fanye
	}
	Return

Clearbm:
	If (!srf_inputing){
		SetYzLogo(srf_mode:=0, 1)
		SetTimer, ToolTipInputStatus, -10
	}
	Gosub srf_value_off
	If (srf_mode&&Escfg=3){
		SetYzLogo(srf_mode:=0, 1)
		SetTimer, ToolTipInputStatus, -10
	}
Return


^+F11:: ;修改dpi同时重启一下影子
if (A_ScreenDPI=96)
    Run SetDpi.exe 200
Else
    run SetDpi.exe 100
sleep 5000
Suspend
Gosub srfreload
Return


;防止卡键工具
SolveKeyProblem:
ToolTip,try to up the key
Gosub,WhichButton
Gosub, GetUpAll
Gosub, ReloadAll
Gosub srfreload
Return

WhichButton:
if GetKeyState("Shift")
    MsgBox At least one Shift key is down.
if GetKeyState("Ctrl")
    MsgBox At least one Ctrl key is down.
if GetKeyState("Alt")
    MsgBox At least one Alt key is down.
if GetKeyState("Capslock")
    MsgBox At least one Capslock key is down.
Return
GetUpAll:
SendInput, {Shift up}{Ctrl Up}{CapsLock up}{Alt up}{Space Up}
Loop 26{
	str:= Chr(Asc("a") + A_Index - 1)
    SendInput, {%str% up}
}
loop 10{
num:=A_Index-1
SendInput, {%num% up}
}
Return

up3:
SendInput, {altup}{ctrlup}{ShiftUp}
Return

ReloadAll:
DetectHiddenWindows On
if !(A_IsAdmin || InStr(DllCall("GetCommandLine", "str"), ".exe"" /r"))
	Run % "*RunAs " (s:=A_IsCompiled ? "" : A_AhkPath " /r ") """" A_ScriptFullPath """" (s ? "" : " /r")
selfPID := DllCall("GetCurrentProcessId")
	WinGet, id, List, ahk_class AutoHotkey
	Loop, %id% {
		this_id := id%A_Index%
		WinGet, this_pid, PID, ahk_id %this_id%
		if (this_pid != selfPID)
		{
			WinGetTitle, this_title, ahk_id %this_id%
				fPath := RegExReplace(this_title, " - AutoHotkey v[\d.]+$")
                PostMessage, 0x111, 65303,,, % fPath " ahk_pid " this_pid
		}
    }
    Return
CreateWords:
	srf_create(RegExReplace(srf_all_Input,"i)[^a-z]"))
Return	

; F4::MsgBox,% A_ScriptDir
; #Include,.\Data\DoubleCopyMenu.ahk
#Include, .\Data\hzn_PluginManager.ahk



;*************************************一些加强按键       ***********************
; LongPressDeleteLine:
; KeyWait, BackSpace, T0.5
; if (ErrorLevel = 1) {
; Gosub, Clearbm
; }
; Return